package com.example.mobile2030606162zwh0514;

import android.app.DownloadManager;
import android.app.VoiceInteractor;
import android.os.Bundle;
import android.telecom.Call;
import android.util.Log;
import android.view.textclassifier.TextLinks;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainContent;
    private String url = "http://192.168.29.230:3000/news";
    private final OKHttpClient okHttpClient = new OKHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(url)
                        .build();

                Call call = okHttpClient.newCall(request);


                try {
                    Response response = call.execute();
                    String result = response.body().string();
                    Log.i("TAG", result);


                } catch (Exception e) {
                    e.printStackTrace();
                }

                call.enqueue(new Callback() {
                    public void onFailure(Call call, IOException e) {

                    }

                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvMainContent.setText(result);
                            }
                        });
                    }

                });
            }
        }).start();
    }

    private void initView() {
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}
